#include <QDebug>
#include <time.h>
#include <QTime>
#include <QWidget>
#include <QtXml>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QXmlStreamReader>
#include <QFile>
#include <QFileDialog>
#include <QTreeWidget>
#include <QTextEdit>
#include <QByteArray>

#include "Interface/UserInterface/_user_unionsystem_interface.h"
#include "Interface/UserInterface/unionsystemdll_structural_define.h"
#include "WorkThreadManage/HmiThreadManage.h"
#include "ui_mainwindow.h"
#include "CommunicationMange/SimpleTcpClient/simpletcpsocketdll_global.h"

#include "UnionSystemCMD/cmd_homepage_new/cmd_homepage_new.h"

void HmiThread ::onbtnclicked_HomeNew(){
    cmd_homepage_new pcmd_homepage_new;
    QString curItemDir = pDocumentModel->filePath(this->ui->DocumnetTree->selectionModel()->selectedIndexes().first());
    pcmd_homepage_new.CurDir = curItemDir;
    pcmd_homepage_new.exec();
}

void HmiThread ::onbtnclicked_HomeOpen(){
    QString FileDir = pDocumentModel->filePath(this->ui->DocumnetTree->selectionModel()->selectedIndexes().first());
    QFile ScriptFile(FileDir);
    if(ScriptFile.exists()){
        if(ScriptFile.open(QIODevice::ReadOnly)) {
            QDomDocument dom("ScriptXML");
            if (dom.setContent(&ScriptFile)){
                ui->ScriptTree->clear();
                ui->ScriptTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
                QDomElement docElem = dom.documentElement();
                RecurrenceAnalysisScriptXML(docElem, NULL);
                ui->ScriptTree->expandAll();
            }
        }
        this->ui->Main->setCurrentIndex(1);
        ScriptFile.close();
        this->ui->Run->setEnabled(false);
    }
    else{
        this->ui->Main->setCurrentIndex(0);
        this->ui->Run->setEnabled(false);
    }

}

void HmiThread ::onbtnclicked_HomeSelect(){
    QString FileDir = pDocumentModel->filePath(this->ui->DocumnetTree->selectionModel()->selectedIndexes().first());
    QFile ScriptFile(FileDir);
    if(ScriptFile.exists()){
        if(ScriptFile.open(QIODevice::ReadOnly)) {
            QDomDocument dom("ScriptXML");
            if (dom.setContent(&ScriptFile)){
                ui->ScriptTree->clear();
                ui->ScriptTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
                QDomElement docElem = dom.documentElement();
                RecurrenceAnalysisScriptXML(docElem, NULL);
                ui->ScriptTree->expandAll();
            }
        }
        this->ui->Main->setCurrentIndex(1);
        ScriptFile.close();
    }
    else{
        this->ui->Main->setCurrentIndex(0);
        this->ui->Run->setEnabled(false);
    }


}

void HmiThread ::RecurrenceAnalysisScriptXML(QDomElement &docElem, QTreeWidgetItem *pItem){
    QDomNode node = docElem.firstChild();
    if(node.toElement().isNull()){
        if( pItem ){
               pItem->setText (1, docElem.text());
           }
    }
    if(!docElem.attributes().isEmpty()){
        QDomNamedNodeMap map = docElem.attributes();
        for(int i = 0 ; i < map.length() ; ++i){
            if(!(map.item(i).isNull())){
                QDomNode debug = map.item(i);
                QDomAttr attr = debug.toAttr();
                if(!attr.isNull()){
                    if( pItem ){
                        QTreeWidgetItem *item;
                        pItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt:: ItemIsSelectable);
                        item = new QTreeWidgetItem(pItem);
                        item->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt:: ItemIsSelectable);
                        item->setText(0, attr.name());
                        item->setText (1, attr.value());
                    }
                }
            }
        }
    }
    while(!node.isNull()){
        QDomElement element = node.toElement();
        if(!element.isNull()){
            QTreeWidgetItem *item;
            if( pItem ){
                pItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt:: ItemIsSelectable);
                item = new QTreeWidgetItem(pItem);
            }
            else{
                item = new QTreeWidgetItem(ui->ScriptTree);
            }
            item->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt:: ItemIsSelectable);
            item->setText(0, element.tagName());
            RecurrenceAnalysisScriptXML(element, item);
            if( pItem ){
                pItem->addChild(item);
            }
            else{
                ui->ScriptTree->addTopLevelItem(item);
            }
            if(element.tagName() == "PluginName"){
                 TecPluginDir = element.text();
                 this->ui->Run->setEnabled(true);
            }
        }
            node = node.nextSibling();
    }
}

